package com.yanggu.payment.feign.weixin_pay;

import com.alibaba.fastjson.JSONObject;
import com.yanggu.payment.config.WeiXinPayFeignConfig;
import com.yanggu.payment.entity.weixinpay.req.WechatAppPayCloseParams;
import com.yanggu.payment.entity.weixinpay.req.WechatAppPrepayParams;
import com.yanggu.payment.entity.weixinpay.req.WeiXinPayApplyRefundReq;
import com.yanggu.payment.entity.weixinpay.resp.WechatAppQueryResult;
import com.yanggu.payment.entity.weixinpay.resp.WeiXinPayApplyRefundResp;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2022/5/5 16:06
 * @description: 微信支付接口
 */
@FeignClient(name = "weiXinPayFeign", url = "${wxpay.domain}", configuration = WeiXinPayFeignConfig.class)
public interface WeiXinPayFeign {

    /**
     * Native下单API
     * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_1.shtml
     * 响应示例
     {
     "code_url": "weixin://wxpay/bizpayurl?pr=p4lpSuKzz"
     }
     * @param req
     * @return
     */
    @PostMapping("/v3/pay/transactions/native")
    JSONObject nativeOrderApi(@RequestBody WechatAppPrepayParams req);

    /**
     * 查询订单API
     * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_2.shtml
     *
     * @param outTradeNo 商户订单号
     * @param mchId      商户id
     * @return o
     */
    @GetMapping("/v3/pay/transactions/out-trade-no/{out_trade_no}")
    WechatAppQueryResult queryForOutTradeNo(@PathVariable("out_trade_no") String outTradeNo,
                                            @RequestParam("mchid") String mchId);

    /**
     * 申请退款API
     * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_9.shtml
     */
    @PostMapping("/v3/refund/domestic/refunds")
    WeiXinPayApplyRefundResp applyRefund(@RequestBody WeiXinPayApplyRefundReq req);

    /**
     * 查询单笔退款API
     * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_10.shtml
     *
     * @param refundNo
     * @return
     */
    @GetMapping("/v3/refund/domestic/refunds/{out_refund_no}")
    WeiXinPayApplyRefundResp queryRefundNo(@PathVariable("out_refund_no") String refundNo);

    /**
     * 关闭订单API
     * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_3.shtml
     *
     * @param orderNo
     */
    @PostMapping("/v3/pay/transactions/out-trade-no/{out_trade_no}/close")
    void cancelOrder(@PathVariable("out_trade_no") String orderNo,
                     @RequestBody WechatAppPayCloseParams params);


    /**
     * 申请交易账单API和申请资金账单API
     * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_6.shtml
     * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_7.shtml
     * <p>
     * 响应示例
     * {
     * "download_url": "https://api.mch.weixin.qq.com/v3/billdownload/file?token=6XIv5TUPto7pByrTQKhd6kwvyKLG2uY2wMMR8cNXqaA_Cv_isgaUtBzp4QtiozLO",
     * "hash_type": "SHA1",
     * "hash_value": "8823044c286bea726f149bfcfce0b0318122d755"
     * }
     *
     * @param type     类型
     * @param billDate
     * @return
     */
    @GetMapping("/v3/bill/{type}")
    JSONObject tradebill(@PathVariable("type") String type,
                         @RequestParam("bill_date") String billDate);

    /**
     * 下载账单API。下载账单API为通用接口，交易/资金账单都可以通过该接口获取到对应的账单。
     * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_8.shtml
     * 这个不需要进行响应签名验证
     *
     * @param token
     * @return
     */
    @GetMapping("/v3/billdownload/file")
    @WeiXinPayIgnoreValidaResponse
    String downloadBill(@RequestParam("token") String token);

}
